package com.iha.pongzor.logic;

import java.io.IOException;

import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.MediaPlayer;

public class myMediaPlayer {

	private static MediaPlayer mediaPlayer;
	AssetFileDescriptor descriptor;
	AssetManager assetManager;

	public myMediaPlayer(AssetManager _assetManager) {
		if (mediaPlayer == null)
			mediaPlayer = new MediaPlayer();

		assetManager = _assetManager;
	}

	public void stop() {
		if (mediaPlayer.isPlaying()) {
			mediaPlayer.pause();
			mediaPlayer.stop();
			mediaPlayer.reset();
		}
	}

	public void start() {
		if (!mediaPlayer.isPlaying()) {
			try {
				mediaPlayer.setDataSource(descriptor.getFileDescriptor(),
						descriptor.getStartOffset(), descriptor.getLength());
				mediaPlayer.prepare();
			} catch (Exception e) {
				e.printStackTrace();
			}

			mediaPlayer.setLooping(true);
			mediaPlayer.start();
		}
	}

	public void changeNumber(String filename) {
		try {
			descriptor = assetManager.openFd(filename);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void destroy() {
		mediaPlayer.stop();
		mediaPlayer.release();
		mediaPlayer = null;
	}
}
